#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: send_to_beanstalk.py
@time: 2018/1/6 18:33
"""
import json
import sys

sys.path.append('..')
sys.path.append('../..')
from common.pybeanstalk import PyBeanstalk
from config.beanstalk_conf import BEANSTALK_CONF

from common.mongo import MongDb
from config.mongo_conf import MONGO_DB_TARGET

# 日志模块
from logger import Logger

log = Logger('send_to_beanstalk.log').get_logger()

target_db = MongDb(MONGO_DB_TARGET['host'], MONGO_DB_TARGET['port'], MONGO_DB_TARGET['db'],
                   MONGO_DB_TARGET['username'],
                   MONGO_DB_TARGET['password'], log=log)

# 初始化监听消息队列
beanstalk = PyBeanstalk(BEANSTALK_CONF['host'], BEANSTALK_CONF['port'])


def main():
    output_tube = 'gsxt_detail_parse'
    table_name = 'new_gansu_detail_page'
    for item in target_db.traverse_batch(table_name, {}):
        if '__change_info' not in item:
            continue
        _id = item.get('_id')
        result = beanstalk.put(output_tube, json.dumps(item))
        if result is None:
            log.error("数据太大: company = {} ".format(_id))
            continue

        log.info("数据发送成功: company = {} ".format(_id))

    log.info("数据发送完成...")


if __name__ == '__main__':
    main()
